package cn.xorange.commons.quartz.controller;

import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.BaseController;
import cn.xorange.commons.quartz.domain.SysJob;
import cn.xorange.commons.quartz.domain.SysJobLog;
import cn.xorange.commons.quartz.service.ISysJobLogService;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.utils.poi.excel.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 调度日志操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseController
{
    @Autowired
    private ISysJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @Authorize(hasPerm = "monitor:job:list")
    @GetMapping("/list")
    public PageBean list(SysJobLog sysJobLog)
    {
        startPage();
        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
        return new PageBean(list);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @Authorize(hasPerm = "monitor:job:export")
    //@Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobLog sysJobLog) throws IOException {
        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
        EasyExcelUtil.exportExcel(list,"导出","导出", SysJobLog.class,"调度日志",response);
    }
    
    /**
     * 根据调度编号获取详细信息
     */
    @Authorize(hasPerm = "monitor:job:query")
    @GetMapping(value = "/{jobLogId}")
    public ResultBean getInfo(@PathVariable Long jobLogId)
    {
        return ResultBean.onSuccess(jobLogService.selectJobLogById(jobLogId));
    }


    /**
     * 删除定时任务调度日志
     */
    @Authorize(hasPerm = "monitor:job:remove")
    //@Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobLogIds}")
    public ResultBean remove(@PathVariable Long[] jobLogIds)
    {
        return ResultBean.onSuccess(jobLogService.deleteJobLogByIds(jobLogIds));
    }

    /**
     * 清空定时任务调度日志
     */
    @Authorize(hasPerm = "monitor:job:remove")
    //@Log(title = "调度日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public ResultBean clean()
    {
        jobLogService.cleanJobLog();
        return ResultBean.onSuccess();
    }
}
